package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type SellOrder struct {
	mgo.BaseDoc   `bson:"-" json:"-"`
	Id            int64               `json:"id" bson:"id,omitempty"`
	Sn            string              `json:"sn" bson:"sn"`                       //单据编号
	Title         string              `json:"title" bson:"title"`                 //单据主题
	Buyer         *Buyer              `json:"buyer" bson:"buyer"`                 //客户
	BuyerId       int64               `json:"buyerId" bson:"buyerId"`             //客户
	BuyerName     string              `json:"buyerName" bson:"buyerName"`         //客户名称
	TaxType       *TaxType            `json:"taxType" bson:"taxType"`             //发票类型
	TaxTypeId     int64               `json:"taxTypeId" bson:"taxTypeId"`         //发票类型
	PayType       *PayType            `json:"payType" bson:"payType"`             //付款方式
	PayTypeId     int64               `json:"payTypeId" bson:"payTypeId"`         //付款方式
	PayCashType   *PayCashType        `json:"payCashType" bson:"payCashType"`     //结算方式
	PayCashTypeId int64               `json:"payCashTypeId" bson:"payCashTypeId"` //结算方式
	DeliverType   *DeliverType        `json:"deliverType" bson:"deliverType"`     //交货方式
	DeliverTypeId int64               `json:"deliverTypeId" bson:"deliverTypeId"` //交货方式
	DeliverAt     time.Time           `json:"deliverAt" bson:"deliverAt"`         //交货时间
	Deliver       string              `json:"deliver" bson:"deliver"`             //交货地点
	ContactEmp    *Emp                `json:"contactEmp" bson:"contactEmp"`       //业务员
	ContactEmpId  int64               `json:"contactEmpId" bson:"contactEmpId"`   //业务员
	Contact       string              `json:"contact" bson:"contact"`             //联系人
	ContactPhone  string              `json:"contactPhone" bson:"contactPhone"`   //联系电话
	Num           float64             `json:"num" bson:"num"`                     //数量
	Money         float64             `json:"money" bson:"money"`                 //金额
	Status        *SellOrderStatus    `json:"status" bson:"status"`               //状态
	StatusId      int64               `json:"statusId" bson:"statusId"`           //状态
	IsConfirm     bool                `json:"isConfirm" bson:"isConfirm"`         //是否生效
	ConfirmAt     time.Time           `json:"confirmAt" bson:"confirmAt"`         //生效时间
	IsCancel      bool                `json:"isCancel" bson:"isCancel"`           //是否取消
	IsLock        bool                `json:"isLock" bson:"isLock"`               //是否锁定
	ApprovalSate  string              `json:"approvalSate" bson:"approvalSate"`   //评审状态
	ApprovalStage string              `json:"approvalStage" bson:"approvalStage"` //评审环节
	FormAt        time.Time           `json:"formAt" bson:"formAt"`               //制单时间
	FormEmp       *Emp                `json:"formEmp" bson:"formEmp"`             //制单人
	FormEmpId     int64               `json:"formEmpId" bson:"formEmpId"`         //制单人
	CreateEmp     *Emp                `json:"createEmp" bson:"createEmp"`         //创建人
	CreateEmpId   int64               `json:"createEmpId" bson:"createEmpId"`     //创建人
	Note          string              `json:"note" bson:"note"`                   //备注
	Product       []*SellOrderProduct `json:"product" bson:"product"`             //产品
	CreateAt      time.Time           `json:"createAt" bson:"createAt"`
	UpdateAt      time.Time           `json:"updateAt" bson:"updateAt"`
	DeleteAt      *time.Time          `json:"deleteAt" bson:"deleteAt"`
}

func (this *SellOrder) DocName() string { return "SellOrder" }
func (this *SellOrder) GetId() int64    { return this.Id }
func (this *SellOrder) SetId(id int64)  { this.Id = id }
func (this *SellOrder) Create() error {
	return this.GetFactory().Create(this)
}
func (this *SellOrder) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *SellOrder) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *SellOrder) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *SellOrder) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *SellOrder) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *SellOrder) ToString() string {
	return string(this.ToBytes())
}
func (this *SellOrder) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellOrder) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *SellOrder) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *SellOrder) LoadBuyer() {
	if this.BuyerId == 0 {
		return
	}
	this.Buyer, _ = NewBuyerCrud().FindId(this.BuyerId)
}
func (this *SellOrder) LoadTaxType() {
	if this.TaxTypeId == 0 {
		return
	}
	this.TaxType, _ = NewTaxTypeCrud().FindId(this.TaxTypeId)
}
func (this *SellOrder) LoadPayType() {
	if this.PayTypeId == 0 {
		return
	}
	this.PayType, _ = NewPayTypeCrud().FindId(this.PayTypeId)
}
func (this *SellOrder) LoadPayCashType() {
	if this.PayCashTypeId == 0 {
		return
	}
	this.PayCashType, _ = NewPayCashTypeCrud().FindId(this.PayCashTypeId)
}
func (this *SellOrder) LoadDeliverType() {
	if this.DeliverTypeId == 0 {
		return
	}
	this.DeliverType, _ = NewDeliverTypeCrud().FindId(this.DeliverTypeId)
}
func (this *SellOrder) LoadContactEmp() {
	if this.ContactEmpId == 0 {
		return
	}
	this.ContactEmp, _ = NewEmpCrud().FindId(this.ContactEmpId)
}
func (this *SellOrder) LoadStatus() {
	if this.StatusId == 0 {
		return
	}
	this.Status, _ = NewSellOrderStatusCrud().FindId(this.StatusId)
}
func (this *SellOrder) LoadFormEmp() {
	if this.FormEmpId == 0 {
		return
	}
	this.FormEmp, _ = NewEmpCrud().FindId(this.FormEmpId)
}
func (this *SellOrder) LoadCreateEmp() {
	if this.CreateEmpId == 0 {
		return
	}
	this.CreateEmp, _ = NewEmpCrud().FindId(this.CreateEmpId)
}
func (this *SellOrder) LoadProduct() {
	product, _ := NewSellOrderProductCrud().FindBy(ss.M{"belongToId": this.Id}, []string{"id"}, 0, 0)
	this.Product = product
}
func (this *SellOrder) ClearRelationsBeforeSave() mgo.Doc {
	this.Buyer = nil
	this.TaxType = nil
	this.PayType = nil
	this.PayCashType = nil
	this.DeliverType = nil
	this.ContactEmp = nil
	this.Status = nil
	this.FormEmp = nil
	this.CreateEmp = nil
	this.Product = nil
	return this
}
func neverUsed_SellOrder() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type SellOrderAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *SellOrder
	Session *ez.Session
}

func NewSellOrderAccessControl(model *SellOrder, action string, session *ez.Session) *SellOrderAccessControl {
	ctrl := &SellOrderAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.SellOrderAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
